﻿using STT.Business;
using STT.Entities;
using System;
using System.IO;
using System.Web;
using System.Web.UI;

namespace STT.Admin.UserControls
{
    public partial class ucThemTinTuc : System.Web.UI.UserControl
    {
        ETbTinTuc tintuc;
        int MaTT = -1;
        protected void Page_Load(object sender, EventArgs e)
        {
            ddLoaiTinTuc.Visible = true;
            Methods.LoaiTinTuc(ref ddLoaiTinTuc);
            btThemMoi.Visible = true;
            btLuu.Visible = false;
            btHuy.Visible = true;
            tintuc = null;

            if (Request.QueryString["idtt"] != null)
            {
                LoadTinSua();
                btLuu.Visible = true;

                btThemMoi.Visible = false;
                //btHuy.Visible = false;
            }
        }
        
        void ThongBao(string Loi)
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "Thông báo!", "<script>alert('" + Loi + "')</script>", false);
        }
        void LoadTinSua()
        {
            try
            {
                MaTT = int.Parse(Request.QueryString["idtt"].ToString());
                tintuc = BTbTinTuc.SelectByID(MaTT);

                if (tintuc.MaLoaiTT > 5)
                    ddLoaiTinTuc.Visible = false;
                else
                    ddLoaiTinTuc.SelectedValue = tintuc.MaLoaiTT.ToString();

                if (tintuc.AnhDaiDien == "")
                    anhDaiDien.ImageUrl = "~/Uploads/Images/default.png";
                else
                    anhDaiDien.ImageUrl = "~/Uploads/Images/" + tintuc.AnhDaiDien;

                txtTieuDeTT.Text = HttpUtility.HtmlDecode(tintuc.TieuDe);
                txtTomTat.Text = HttpUtility.HtmlDecode(tintuc.NDTomTat);
                txtChiTiet.Text = HttpUtility.HtmlDecode(tintuc.NDChiTiet);
            }
            catch { }
        }
        protected void btThemMoi_Click(object sender, EventArgs e)
        {          
             try
            {
                string strAnhDaiDien="";
                if (upAnhDaiDien.FileName != "")
                {
                    strAnhDaiDien = upAnhDaiDien.FileName.ToString();
                    strAnhDaiDien = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + Guid.NewGuid().ToString() + "¿" + strAnhDaiDien;
                    upAnhDaiDien.SaveAs(Path.Combine(Server.MapPath("~/Uploads/Images/"), strAnhDaiDien));
                }                               

                BTbTinTuc.Insert(new ETbTinTuc
                {
                    TieuDe = HttpUtility.HtmlEncode(txtTieuDeTT.Text),
                    NDTomTat = HttpUtility.HtmlEncode(txtTomTat.Text),
                    AnhDaiDien = strAnhDaiDien,
                    NDChiTiet = txtChiTiet.Text,
                    MaLoaiTT = int.Parse(ddLoaiTinTuc.SelectedValue),
                    NguoiDang = Session["TenDN"].ToString(),
                    NgayDang = DateTime.Now,
                    LinkFile = ""
                });

                if (ddLoaiTinTuc.SelectedValue == "2")
                {
                    BTbHocVien.UpdateTB();
                }else
                    if (ddLoaiTinTuc.SelectedValue == "3")
                    {
                        BTbGiangVien.UpdateTB();
                    }
                Session["action"] = 1;
                Response.Redirect("~/Admin/Default.aspx?idpage=qlttc", true);                
            }
             catch { }
        }

        protected void btSua_Click(object sender, EventArgs e)
        {
            try
            {
                MaTT = int.Parse(Request.QueryString["idtt"].ToString());                

                if (txtTieuDeTT.Text.Trim() == "")
                {
                    ThongBao("Tiêu đề không được để trống.");
                    return;
                }               

                string strAnhDaiDien = tintuc.AnhDaiDien;
                if (upAnhDaiDien.FileName != "")
                {
                    strAnhDaiDien = upAnhDaiDien.FileName.ToString();
                    strAnhDaiDien = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + Guid.NewGuid().ToString() + "¿" + strAnhDaiDien;
                    upAnhDaiDien.SaveAs(Path.Combine(Server.MapPath("~/Uploads/Images/"), strAnhDaiDien));
                }

                BTbTinTuc.Update(new ETbTinTuc
                {
                    MaTT = MaTT,
                    TieuDe = HttpUtility.HtmlEncode(txtTieuDeTT.Text),
                    NDTomTat = HttpUtility.HtmlEncode(txtTomTat.Text),
                    AnhDaiDien = strAnhDaiDien,
                    NDChiTiet = txtChiTiet.Text,
                    MaLoaiTT = !ddLoaiTinTuc.Visible ? tintuc.MaLoaiTT : int.Parse(ddLoaiTinTuc.SelectedValue),
                    NguoiDang = Session["TenDN"].ToString(),
                    NgayDang = DateTime.Now,
                    LinkFile = ""//strFile
                });
                Session["action"] = 2;
                //txtChiTiet.Text = HttpUtility.HtmlDecode(txtChiTiet.Text);         
                Response.Redirect("~/Admin/Default.aspx?idpage=qlttc", true);
               // LoadTinSua();
            }
            catch { }
        }

        protected void btHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Admin/Default.aspx?idpage=qlttc", true);
        }
    }
}